package com.db.patterns.factory.demo03;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class OperationFactory {
    private static Map<String, Operation> operations = new HashMap<String, Operation>();

    static {
        Properties properties = new Properties();
        try {
            InputStream inputStream = OperationFactory.class.getClassLoader().getResourceAsStream("data.properties");
            properties.load(inputStream);
            for (Object key : properties.keySet()) {
                Object value = properties.get(key);
                Class<?> aClass = Class.forName(value.toString());
                Operation operation = (Operation) aClass.newInstance();
                operations.put(key.toString(), operation);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Operation getOperation(String operation) {
        return operations.get(operation);
    }
}
